package com.example.unisys.utils;

import com.alibaba.fastjson.JSONObject;
import com.example.unisys.entity.StuCourse;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZipUtil {

    public static void unZipFiles(File zipFile, String descDir)throws IOException
    {
        File pathFile = new File(descDir);
        if(!pathFile.exists())
        {
            pathFile.mkdirs();
        }
        //解决zip文件中有中文目录或者中文文件
        ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
        for(Enumeration entries = zip.entries(); entries.hasMoreElements();)
        {
            ZipEntry entry = (ZipEntry)entries.nextElement();
            String zipEntryName = entry.getName();
            InputStream in = zip.getInputStream(entry);
            String outPath = descDir+File.separator+zipEntryName;
            System.out.println("outpath is "+outPath);
            System.out.println("zipentryname is "+zipEntryName);
            //判断路径是否存在,不存在则创建文件路径
            File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));


            if(!file.exists())
            {
                file.mkdirs();
            }
             File tmpfile = new File(outPath);
            if(tmpfile.exists())
                tmpfile.delete();

            //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
            if(new File(outPath).isDirectory())
            {
                continue;
            }
            //输出文件路径信息
            System.out.println(outPath);
            OutputStream out = new FileOutputStream(outPath);
            byte[] buf1 = new byte[1024];
            int len;
            while((len=in.read(buf1))>0)
            {
                out.write(buf1,0,len);
            }
            in.close();
            out.close();
        }
        zip.close(); //**原博忘记关了,所以我在做完这些操作后想要删除文件时,一直已占用,必须关掉服务器才可以**
        System.out.println("******************解压完毕********************");
    }


    public static void main(String[] args){
      /*  String filepath="D:\\photo.zip";
        System.out.println("\\*");
        File file=new File(filepath);
        String outpath="D:\\clean";
        try {
            ZipUtil.unZipFiles(file, outpath);
        }catch(Exception e){
            e.printStackTrace();
        }*/


      String json="{\n" +
              "\t\"exam_id\":\"119\",\n" +
              "\t\"course_id\":\"12\",\n" +
              "\t\"catalogtree\":{\"catalogtree\":[{\"children\":[{\"children\":[{\"vid\":\"1f875362c362a7353d640f6adec837ae_1\",\"finished\":false,\"id\":3,\"label\":\"饮料概述\",\"type\":true}],\"id\":2,\"label\":\"饮料概述\",\"type\":false}],\"id\":1,\"label\":\"第一章 饮料概述\",\"type\":false},{\"children\":[{\"children\":[{\"vid\":\"1f875362c341682bea8ffe4b1ffd5311_1\",\"finished\":false,\"id\":6,\"label\":\"酒类饮料概述\",\"type\":true}],\"id\":5,\"label\":\"酒类饮料概述\",\"type\":false}],\"id\":4,\"label\":\"第二章 酒类饮料概述\",\"type\":false},{\"children\":[{\"children\":[{\"vid\":\"1f875362c394217b5786a3814ab7ccd7_1\",\"finished\":false,\"id\":9,\"label\":\"发酵酒\",\"type\":true}],\"id\":8,\"label\":\"发酵酒\",\"type\":false}],\"id\":7,\"label\":\"第三章 发酵酒\",\"type\":false},{\"children\":[{\"children\":[{\"vid\":\"1f875362c3aac6506524b572fff8ff28_1\",\"finished\":false,\"id\":12,\"label\":\"蒸馏酒\",\"type\":true}],\"id\":11,\"label\":\"蒸馏酒\",\"type\":false}],\"id\":10,\"label\":\"第四章 蒸馏酒\",\"type\":false},{\"children\":[{\"children\":[{\"vid\":\"1f875362c30d379a2a5e1d8c33cd3b7b_1\",\"finished\":false,\"id\":15,\"label\":\"配制酒\",\"type\":true}],\"id\":14,\"label\":\"配制酒\",\"type\":false}],\"id\":13,\"label\":\"第五章 配制酒\",\"type\":false},{\"children\":[{\"children\":[{\"vid\":\"1f875362c30d379a2a5e1d8c33cd3b7b_1\",\"finished\":false,\"id\":18,\"label\":\"鸡尾酒与混合饮品\",\"type\":true}],\"id\":17,\"label\":\"鸡尾酒与混合饮品\",\"type\":false}],\"id\":16,\"label\":\"第六章 鸡尾酒与混合饮品\",\"type\":false},{\"children\":[{\"children\":[{\"vid\":\"1f875362c30d379a2a5e1d8c33cd3b7b_1\",\"finished\":false,\"id\":21,\"label\":\"酒吧服务\",\"type\":true}],\"id\":20,\"label\":\"酒吧服务\",\"type\":false}],\"id\":19,\"label\":\"第七章 酒吧服务\",\"type\":false},{\"children\":[{\"children\":[{\"vid\":\"1f875362c386fa895fda622372a20497_1\",\"finished\":false,\"id\":24,\"label\":\"酒单的设计与制作\",\"type\":true}],\"id\":23,\"label\":\"酒单的设计与制作\",\"type\":false}],\"id\":22,\"label\":\"第八章 酒单的设计与制作\",\"type\":false},{\"children\":[{\"children\":[{\"vid\":\"1f875362c343e260f156e160b2d4dfa9_1\",\"finished\":false,\"id\":27,\"label\":\"酒水经营与成本控制\",\"type\":true}],\"id\":26,\"label\":\"酒水经营与成本控制\",\"type\":false}],\"id\":25,\"label\":\"第九章 酒水经营与成本控制\",\"type\":false}]}\n" +
              "\t\n" +
              "}";


      StuCourse stucourse = JSONObject.parseObject(json, StuCourse.class);
        System.out.println(stucourse.toString());
    }


}
